<?php 

namespace app\transfer\controller;

use app\common\controller\Common;
use think\Db;
use app\common\MixDataHandle;
use app\common\Content;
use app\api\model\SeparateToFile;
use app\api\model\LiveSpeech1;
// use app\common\cache\RedisPackage;

class SpeechLogic extends Common {
	// 获取说话人分离结果
	public function separateShowLogic($fid, $num=2) {
        $result = [];
        $separate = SeparateToFile::get(['fid' => $fid, 'num' => $num]);
        if (!$separate) {
            // return resultArray(['data' => '']);
            return operationRes(true, '');
        }
        if($separate->separate){
            $contents = file_get_contents($separate->separate);
            $filecontent = new Content();
            $FRAME = 0.2;
            $speech_list = json_decode($contents, true)['list'];
            if (!$speech_list) {
                $speech_list = [];
            }
            foreach ($speech_list as $sentence) {
                $filecontent->push($sentence['txt'], (explode("-", $sentence['time'])[1] - explode("-", $sentence['time'])[0]) * $FRAME, $sentence['speaker']);
            }
            $result['separate'] = $filecontent->paragraph()->getArr();
            // for ($i = 0; $i < count($result['separate']); $i++) {
            //     $result['separate'][$i]['id'] = $i;
            // }
            //过滤空语音
            $temp = [];
            for ($i = 0, $j=0; $i < count($result['separate']); $i++) {
                if($result['separate'][$i]['txt']){
                    $result['separate'][$i]['id'] = $j++;
                    array_push($temp, $result['separate'][$i]);
                }
            }
            $result['separate'] = $temp;
        }
        $result['status'] = $separate->status;
        // return resultArray(['data' => $result]);
        return operationRes(true, '', $result);
    }

    //开始说话人分离功能
    public function startRoleSeparate($fid, $num=2) {
        try{
            Db::startTrans();
            $param = [
                'fid' => $fid,
                'num' => $num,
            ];
            if($res = Db::name('separate_to_file')->where($param)->find()){
                $result = Db::name('separate_to_file')->where($param)->where('status','<>',0)->update(['status' => 0, 'separate' => '']);
                $id = $res['id'];
                if(!$result){
                    Db::commit();
                    // return resultArray(['data' => ""]);
                    return operationRes(true, '');
                }
            }else{
                $param['separate'] = '';
                $id = Db::name('separate_to_file')->insertGetId($param);
            }
            $speech = LiveSpeech1::get($fid);
            $role_arr = [
                'type' => 1,
                'id' => $id,
                'fid' => $fid,
                'num' => $num,
                'file' => config('huisheng.box_audio_dir') . DS . $speech->formatname . ".wav",
            ];
            self::$redis->roleSeparatePush($role_arr);
            Db::commit();
            // return resultArray(['data' => ""]);
            return operationRes(true, '');
        }catch(\Exception $e){
            Db::rollback();
            // return resultArray(['error' => $e->getMessage()]);
            return operationRes(false, $e->getMessage());
        }
    }
}